<?php

function nav($arr = 0) {
    $arr = explode(",", $arr);
    if (in_array(0, $arr)) {
        $menu = M('Category')->order('order_id desc')->select();
    } else {
        $where['id'] = array('IN', $arr);
        $menu = M('Category')->where($where)->order('order_id desc')->select();
    }
    $menu = toLevel($menu, '&nbsp;&nbsp;&nbsp;&nbsp;', 0, 0);
    foreach ($menu as $key => $value) {
        switch ($value['level']) {
            case 1:
                switch ($value['model_id']) {
                    case 1:
                        if ($value['sub'] == 1) {
                            $nav .= '<dt><a href="javascript:;" target="main">' . $value["name"] . '</a></dt><dd>';
                        } else {
                            $nav .= '<dt><a href="' . U('Page/index', array('category_id' => $value["id"])) . '" target="main">' . $value["name"] . '</a></dt>';
                        }
                        break;
                    case 2:
                        if ($value['sub'] == 1) {
                            $nav .= '<dt><a href="javascript:;" target="main">' . $value["name"] . '</a></dt><dd>';
                        } else {
                            $nav .= '<dt><a href="' . U('Article/index', array('category_id' => $value["id"])) . '" target="main">' . $value["name"] . '</a></dt>';
                        }
                        break;
                    case 3:
                        if ($value['sub'] == 1) {
                            $nav .= '<dt><a href="javascript:;" target="main">' . $value["name"] . '</a></dt><dd>';
                        } else {
                            $nav .= '<dt><a href="' . U('Products/index', array('category_id' => $value["id"])) . '" target="main">' . $value["name"] . '</a></dt>';
                        }
                        break;
                    case 4:
                        if ($value['sub'] == 1) {
                            $nav .= '<dt><a href="javascript:;" target="main">' . $value["name"] . '</a></dt><dd>';
                        } else {
                            $nav .= '<dt><a href="' . U('Activity/index', array('category_id' => $value["id"])) . '" target="main">' . $value["name"] . '</a></dt>';
                        }
                        break;
                }
                break;
            case 2:
                switch ($value['model_id']) {
                    case 1:
                        if ($value['sub'] == 1) {
                            $nav .= '<p>☉<a href="javascript:;" target="main">' . $value["name"] . '</a></p>';
                        } else {
                            $nav .= '<p>☉<a href="' . U('Page/index', array('category_id' => $value["id"])) . '" target="main">' . $value["name"] . '</a></p>';
                        }
                        break;
                    case 2:
                        if ($value['sub'] == 1) {
                            $nav .= '<p>☉<a href="javascript:;" target="main">' . $value["name"] . '</a></p>';
                        } else {
                            $nav .= '<p>☉<a href="' . U('Article/index', array('category_id' => $value["id"])) . '" target="main">' . $value["name"] . '</a></p>';
                        }
                        break;
                    case 3:
                        if ($value['sub'] == 1) {
                            $nav .= '<p>☉<a href="javascript:;" target="main">' . $value["name"] . '</a></p>';
                        } else {
                            $nav .= '<p>☉<a href="' . U('Products/index', array('category_id' => $value["id"])) . '" target="main">' . $value["name"] . '</a></p>';
                        }
                        break;
                    case 4:
                        if ($value['sub'] == 1) {
                            $nav .= '<dt><a href="javascript:;" target="main">' . $value["name"] . '</a></dt><dd>';
                        } else {
                            $nav .= '<dt><a href="' . U('Activity/index', array('category_id' => $value["id"])) . '" target="main">' . $value["name"] . '</a></dt>';
                        }
                        break;
                    default:
                        $nav .= "</dd>";
                }
                break;
            default:
                switch ($value['model_id']) {
                    case 1:
                        if ($value['sub'] == 1) {
                            $nav .= '<p>' . $value['delimiter'] . '☉<a href="javascript:;" target="main">' . $value["name"] . '</a></p>';
                        } else {
                            $nav .= '<p>' . $value['delimiter'] . '☉<a href="' . U('Page/index', array('category_id' => $value["id"])) . '" target="main">' . $value["name"] . '</a></p>';
                        }
                        break;
                    case 2:
                        if ($value['sub'] == 1) {
                            $nav .= '<p>' . $value['delimiter'] . '☉<a href="javascript:;" target="main">' . $value["name"] . '</a></p>';
                        } else {
                            $nav .= '<p>' . $value['delimiter'] . '☉<a href="' . U('Article/index', array('category_id' => $value["id"])) . '" target="main">' . $value["name"] . '</a></p>';
                        }
                        break;
                    case 3:
                        if ($value['sub'] == 1) {
                            $nav .= '<p>' . $value['delimiter'] . '☉<a href="javascript:;" target="main">' . $value["name"] . '</a></p>';
                        } else {
                            $nav .= '<p>' . $value['delimiter'] . '☉<a href="' . U('Products/index', array('category_id' => $value["id"])) . '" target="main">' . $value["name"] . '</a></p>';
                        }
                        break;
                    case 4:
                        if ($value['sub'] == 1) {
                            $nav .= '<dt><a href="javascript:;" target="main">' . $value["name"] . '</a></dt><dd>';
                        } else {
                            $nav .= '<dt><a href="' . U('Activity/index', array('category_id' => $value["id"])) . '" target="main">' . $value["name"] . '</a></dt>';
                        }
                        break;
                    default:
                        $nav .= "</dd>";
                }
        }
    }
    $nav .= '<dt><a href="javascript:;">管理员设置</a></dt>';
    $nav .= '<dd>';
    $nav .= '<p>☉<a href="' . U('Manage/password') . '" target="main">管理员信息</a></p>';
    $nav .= '</dd>';
    if (in_array(0, $arr)) {
        $nav .= '<dt><a href="javascript:;">系统设置</a></dt>';
        $nav .= '<dd>';
        $nav .= '<p>☉<a href="' . U('Config/index') . '" target="main">网站设置</a></p>';
        $nav .= '<p>☉<a href="' . U('Category/index') . '" target="main">栏目管理</a></p>';
        $nav .= '<p>☉<a href="' . U('Slide/index') . '" target="main">广告管理</a></p>';
        $nav .= '<p>☉<a href="' . U('Links/index') . '" target="main">友情链接</a></p>';
        $nav .= '<p>☉<a href="' . U('Bak/index') . '" target="main">数据库管理</a></p>';
        $nav .= '</dd>';
    }
    return $nav;
}

function getCategory($category_id) {
    $map['username'] = array('EQ', cookie('yz_name'));
    $typeids = M('Manage')->where($map)->getField('typeids');
    $typeids = explode(",", $typeids);
    if (in_array(0, $typeids)) {
        $where['id'] = array('NEQ', $category_id);
        $where['model_id'] = array('EQ', 2);
        $menu = M('Category')->where($where)->order('order_id desc')->select();
    } else {
        $where['id'] = array(array('IN', $typeids), array('NEQ', $category_id));
        $where['model_id'] = array('EQ', 2);
        $menu = M('Category')->where($where)->order('order_id desc')->select();
    }
    $menu = toLevel($menu, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', 0);
    return $menu;
}

//删除图册图片
function photoDelete($cid) {
    $listPhoto = M('Gallery')->where(array('cid' => $cid))->select();
    foreach ($listPhoto as $value) {
        $img_url = '.' . $value['image_src'];
        //获取缩略图地址        
        $tb_img_url = get_tb_img_url($img_url);
        @unlink($img_url);
        @unlink($tb_img_url);
    }
    $resultPhoto = M('Gallery')->where(array('cid' => $cid))->delete();
}

//获取缩略图地址
function get_tb_img_url($img_src) {
    $info = pathinfo($img_src);
    $path = $info['dirname'];
    $name = $info['basename'];
    $tb_img_src = $path . '/tb_' . $name;
    return $tb_img_src;
}

//Uploadify上传图片
function uploadify($type) {
    if (!empty($_FILES)) {
        //上传参数配置
        $config = array(
            'maxSize' => 10240000, // 设置附件上传大小
            'rootPath' => 'Uploads/', // 设置附件上传根目录
            'savePath' => '', // 设置附件上传（子）目录
            'saveName' => date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8), //保存名称
            'exts' => array('jpg', 'gif', 'png', 'jpeg'), // 设置附件上传类型
            'autoSub' => true, //自动使用子目录保存上传文件 默认为true
            'subName' => array('date', 'Ymd'), //子目录创建方式，采用数组或者字符串方式定义
        );
        $upload = new \Think\Upload($config); // 实例化上传类
        // 上传文件 
        $info = $upload->upload();
        if (!$info) {
            // 上传错误提示错误信息
            $data = array(
                'status' => 0, //状态
                'info' => $upload->getError(), //错误信息
            );
        } else {
            $watermarking = "./Public/Images/watermarking.png"; //水印图片地址
            switch ($type) {
                case 'NP':
                    //无水印单图片
                    $data = array(
                        'status' => 1,
                        'info' => '图片上传成功！',
                        'image_src' => '/' . $config['rootPath'] . $info['Filedata']['savepath'] . $info['Filedata']['savename'],
                    );
                    break;
                case 'WP':
                    //带水印单图片
                    $image_src = $config['rootPath'] . $info['Filedata']['savepath'] . $info['Filedata']['savename']; //图片地址
                    $img = new \Think\Image(1, $image_src);
                    $img->water($watermarking); //添加水印默认是右下角
                    $img->save($image_src); //保存水印后的图片
                    $data = array(
                        'status' => 1, //状态
                        'info' => '图片上传成功！',
                        'image_src' => '/' . $image_src,
                    );
                    break;
                case 'WG':
                    //无缩图图册
                    $image_src = $config['rootPath'] . $info['Filedata']['savepath'] . $info['Filedata']['savename']; //图片地址
                    $img = new \Think\Image(1, $image_src);
                    $img->water($watermarking); //添加水印默认是右下角
                    $img->save($image_src); //保存水印后的图片
                    $data = array(
                        'savename' => $info['Filedata']['savename'], //保存名称
                        'savepath' => '/' . $config['rootPath'] . $info['Filedata']['savepath'], //保存路径
                        'savetitle' => substr($info['Filedata']['savename'], 0, strrpos($info['Filedata']['savename'], '.')),
                        'status' => 1, //状态
                    );
                    break;
                case 'NWG':
                    //无水印无缩图图册
                    $image_src = $config['rootPath'] . $info['Filedata']['savepath'] . $info['Filedata']['savename']; //图片地址
                    $data = array(
                        'savename' => $info['Filedata']['savename'], //保存名称
                        'savepath' => '/' . $config['rootPath'] . $info['Filedata']['savepath'], //保存路径
                        'savetitle' => substr($info['Filedata']['savename'], 0, strrpos($info['Filedata']['savename'], '.')),
                        'status' => 1, //状态
                    );
                    break;
                case 'WGT':
                    //带缩图图册
                    $image_src = $config['rootPath'] . $info['Filedata']['savepath'] . $info['Filedata']['savename']; //图片地址
                    $img = new \Think\Image(1, $image_src);
                    $img->water($watermarking); //添加水印默认是右下角
                    $img->save($image_src); //保存水印后的图片
                    $thumb = $img->thumb(250, 150); //缩略图宽250px 高150px 等比例缩放
                    $thumb_src = $config['rootPath'] . $info['Filedata']['savepath'] . 'tb_' . $info['Filedata']['savename']; //缩略图地址
                    $img->save($thumb_src); //保存缩略图
                    $data = array(
                        'savename' => $info['Filedata']['savename'], //保存名称
                        'savepath' => '/' . $config['rootPath'] . $info['Filedata']['savepath'], //保存路径
                        'savetitle' => substr($info['Filedata']['savename'], 0, strrpos($info['Filedata']['savename'], '.')),
                        'status' => 1, //状态
                    );
                    break;
            }
        }
        return $data;
    }
}

//处理方法
function rmdirr($dirname) {
    if (!file_exists($dirname)) {
        return false;
    }
    if (is_file($dirname) || is_link($dirname)) {
        return unlink($dirname);
    }
    $dir = dir($dirname);
    if ($dir) {
        while (false !== $entry = $dir->read()) {
            if ($entry == '.' || $entry == '..') {
                continue;
            }
            //递归
            rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
        }
    }
}

function getfiletime($file, $DataDir) {
    $a = filemtime($DataDir . $file);
    $time = date("Y-m-d H:i:s", $a);
    return $time;
}

//获取文件的大小
function getfilesize($file, $DataDir) {
    $perms = stat($DataDir . $file);
    $size = $perms['size'];
    // 单位自动转换函数
    $kb = 1024;         // Kilobyte
    $mb = 1024 * $kb;   // Megabyte
    $gb = 1024 * $mb;   // Gigabyte
    $tb = 1024 * $gb;   // Terabyte
    if ($size < $kb) {
        return $size . " B";
    } else if ($size < $mb) {
        return round($size / $kb, 2) . " KB";
    } else if ($size < $gb) {
        return round($size / $mb, 2) . " MB";
    } else if ($size < $tb) {
        return round($size / $gb, 2) . " GB";
    } else {
        return round($size / $tb, 2) . " TB";
    }
}

function qrimg($frame) {
    echo 111;

    require_once __DIR__ . DIRECTORY_SEPARATOR . 'Phpqrcode.php';
    \QRimage::png($frame);
}

/**
 * 功能：生成二维码
 * @paramstring$qr_data手机扫描后要跳转的网址
 * @paramstring$qr_level默认纠错比例分为L、M、Q、H四个等级，H代表最高纠错能力
 * @paramstring$qr_size二维码图大小，1－10可选，数字越大图片尺寸越大
 * @paramstring$save_path图片存储路径
 * @paramstring$save_prefix图片名称前缀
 */
function createQRcode($save_path, $qr_data = 'PHPQRCode:)', $qr_level = 'L', $qr_size = 4, $save_prefix = 'qrcode') {
    if (!isset($save_path)) {
        return '';
    }//设置生成png图片的路径
    $PNG_TEMP_DIR = &$save_path; //导入二维码核心程序
    vendor('PHPQRcode.class#phpqrcode'); //注意这里的大小写哦，不然会出现找不到类，PHPQRcode是文件夹名字，class#phpqrcode就代表class.phpqrcode.php文件名//检测并创建生成文件夹
    if (!file_exists($PNG_TEMP_DIR)) {
        mkdir($PNG_TEMP_DIR);
    }
    $filename = $PNG_TEMP_DIR . 'test.png';
    $errorCorrectionLevel = 'L';
    if (isset($qr_level) && in_array($qr_level, array('L', 'M', 'Q', 'H'))) {
        $errorCorrectionLevel = &$qr_level;
    }
    $matrixPointSize = 4;
    if (isset($qr_size)) {
        $matrixPointSize = & min(max((int) $qr_size, 1), 10);
    }
    if (isset($qr_data)) {
        if (trim($qr_data) == '') {
            die('data can not be empty!');
        }
        //生成文件名文件路径+图片名字前缀+md5(名称)+.png
        $filename = $PNG_TEMP_DIR . $save_prefix . md5($qr_data . '|' . $errorCorrectionLevel . '|' . $matrixPointSize) . '.png';
        //开始生成
        QRcode::png($qr_data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
    } else {
        //默认生成
        QRcode::png('PHPQRCode:)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
    }
    if (file_exists($PNG_TEMP_DIR . basename($filename)))
        return basename($filename);
    else
        return FALSE;
}
?>